<template>
  <div>
    <Breadcrumb></Breadcrumb>
    <!-- 顶部区域 -->
    <div class="header">
      <div class="form">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="预定编号">
            <el-input size="small" placeholder="预定编号"></el-input>
          </el-form-item>
          <el-form-item label="预定时间">
            <el-date-picker size="small" type="date" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="group">
        <el-button size="small" type="warning" @click="orderCollect"
          >订单汇总</el-button
        >
        <download-excel
          class="export-excel-wrapper"
          :data="DetailsForm"
          :fields="json_fields"
          :header="title"
          name="采购公司订单列表.xls"
        >
          <el-button size="small" type="warning">导出</el-button>
        </download-excel>
      </div>
    </div>
    <!-- 表格区域 -->
    <div class="content">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        header-cell-class-name="active-header"
        cell-class-name="table-center"
        @select="select"
        @select-all="selectAll"
      >
        <el-table-column type="selection" width="55" :selectable="selectable">
        </el-table-column>
        <el-table-column prop="code" label="订单编号">
          <template slot-scope="scope">
            <span @click="descflag" style="color: blue; cursor: pointer">{{
              scope.row.code
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ordername" label="下单人"> </el-table-column>
        <el-table-column prop="company" label="所属单位"> </el-table-column>
        <el-table-column prop="phone" label="联系电话"> </el-table-column>
        <el-table-column prop="yudingTime" label="预定时间">
          <!-- <template slot-scope="scope">
            {{ dayjs(scope.row.yudingTime).format("YYYY-MM-DD") }}
          </template> -->
        </el-table-column>
        <el-table-column prop="price" label="订单总价格"> </el-table-column>
        <el-table-column prop="huizongStatus" label="汇总状态">
          <!-- <template slot-scope="scope">
            {{ scope.row.huizongStatus == 1 ? "未汇总" : "已汇总" }}
          </template> -->
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        :pageSize="pageSize"
        @currentPage="currentPage"
      ></Pagination>
    </div>
    <OrderDecs ref="child"></OrderDecs>
  </div>
</template>

<script>
import { orderList, changeHz } from "@/http/orderList.js";
import Pagination from "@/components/pagination/index.vue";
import dayjs from "dayjs";
// import Pagination from '@/components/pagination/Pagination.vue'
import OrderDecs from "./OrderDecs.vue";
export default {
  components: {
    Pagination,
    OrderDecs,
  },
  data() {
    return {
      tableData: [],
      total: 100,
      pageSize: 8,
      ids: [],
      currentpage: "",
      DetailsForm: [], //需要导出的数据 [{},{}]
      json_fields: {
        //映射字段值
        // "订单编号": 'code',
        //数字处理--
        订单编号: {
          field: "code",
          callback: (value) => {
            return "&nbsp;" + value;
          },
        },
        下单人: "ordername",
        所属单位: "company",
        联系电话: "phone",
        预定时间: "yudingTime",
        订单总价格: "price",
        汇总状态: "huizongStatus",
      },
      title: "公司采购系统订单列表", //
    };
  },
  methods: {
    dayjs,
    orderCollect() {
      if (this.ids.length >= 2) {
        changeHz({ ids: this.ids.join(",") }).then((res) => {
          console.log(res);
          if (res.data.status == 200) {
            this.$message({
              type: "success",
              message: "汇总成功!",
            });
            // if(this.total )
            let maxPage = Math.ceil(this.total / this.pageSize);
            let len = this.ids.length;
            if (maxPage == this.currentpage) {
              let num =
                this.total % this.pageSize == 0
                  ? this.pageSize
                  : this.total % this.pageSize;
              if (num == len) {
                this.currentpage =
                  this.currentpage - 1 > 0 ? this.currentpage - 1 : 1;
              }
            }
            this.initData(this.currentpage);
          }
        });
      } else {
        this.$alert("汇总订单至少需要2条订单信息", "汇总订单", {
          confirmButtonText: "确定",
          callback: (action) => {
            this.$message({
              type: "info",
              message: `取消汇总`,
            });
          },
        });
      }
    },
    descflag() {
      this.$refs.child.drawer = true;
    },
    // 禁用按钮设置
    selectable(row, index) {
      // console.log(row, index, "何方宵小");
      if (row.huizongStatus == "未汇总") {
        return true;
      } else {
        return false;
      }
    },
    // 全选的按钮
    selectAll(selection) {
      console.log(selection, "这是全选的内容");
      this.DetailsForm = selection;
    },
    currentPage(page) {
      this.currentpage = page;
      this.initData(page);
    },
    initData(page) {
      orderList({ page })
        .then((res) => {
          console.log(res);
          if (res.data.status == 200) {
            // this.tableData = res.data.data;
            let arr = res.data.data;
            arr.forEach((ele) => {
              ele.huizongStatus = ele.huizongStatus == 1 ? "未汇总" : "已汇总";
              ele.yudingTime = dayjs(ele.yudingTime).format("YYYY-MM-DD");
            });
            this.tableData = arr;
            // this.DetailsForm = this.tableData;
            (this.total = res.data.total), (this.pageSize = res.data.pageSize);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    select(selection, row) {
      this.ids = selection.map((item) => {
        return item.id;
      });
      console.log(this.ids, "选中了个啥玩意儿");
      this.DetailsForm = selection;
    },
  },

  created() {},
  mounted() {
    this.initData(1);
  },
};
</script>


<style lang="scss" scoped>
.header {
  background: #fff;
  margin-bottom: 20px;
  padding: 10px;

  .el-form-item {
    margin-bottom: 16px;
  }

  .group {
    border: 1px solid #eee;
    padding: 8px;
  }
}

.content {
  background: #fff;

  ::v-deep .active-header {
    color: #333;
    text-align: center;
  }

  ::v-deep .table-center {
    text-align: center;
  }

  .pagination {
    padding: 10px;
  }
}
.export-excel-wrapper {
  display: inline-block;
  margin-left: 10px;
  button {
    padding-left: 30px;
    padding-right: 30px;
  }
}
</style>